header {
    package de.dopichaj.labrador.search;

    import java.util.List;
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.Set;
    import de.dopichaj.labrador.search.erg.ElementRelationshipGraph;
}

class NEXIParser extends TreeParser;

options {
    importVocab = NEXILexer;
}

{
    private Query processor;

    public NEXIParser(Query processor) {
    this();
    this.processor = processor;
    }
}

word returns [String r=""]
    : w:WORD { r = w.getText(); }
    ;

phrase returns [List l = new ArrayList()] { String w; }
    : #(QUOTE (w=word { l.add(w); })+)
    ;

unrestricted_term[String context, boolean required, boolean prohibited]
{
    String t;
    List p;
    float boost = required ? 2.0f : 1.0f;
    if (context == null) {
        context = "";
    }
}
    : t=word
    {
        if (!prohibited)
            processor.addTermQuery(context, t, boost);
    }
    | p=phrase
    {
        if (!prohibited)
            processor.addPhraseQuery(context, p, boost);
    }
    ;

term[String context]
    : #(MUST unrestricted_term[context, true, false])
    | #(MUST_NOT unrestricted_term[context, false, true])
    | unrestricted_term[context, false, false]
    ;

node returns [String n=null]
{ String n1, n2; }
    : ANY
    | x:XML_TAG
    {
        n = x.getText();
    }
    | #(PIPE n1=node n2=node)
    { n = n1 + " " + n2; }
    ;

path returns [String p=""]
{ String n = null; }
    : #(NODE_QUALIFIER (p=path)? n=node)
    {
        if (n != null) {
            p = p + " " + n;
        }
    }
    ;

relative_path [String context] returns [String p=""]
{ String sp = null; }
    : #(DOT (sp=path)?)
    { p = sp != null ? context + " " + sp : context; }
    | p=path
    ;

co[String context]
    : #(CO (term[context])+)
    ;

about_clause [String context]
{ String p; }
    : #(ABOUT p=relative_path[context] co[p])
    ;

number returns [int n=0]
    : x:NUMBER { n = Integer.parseInt(x.getText()); }
    ;

arithmetic_clause [String context]
    : #(ARITHMETIC_OPERATOR relative_path[context] number)
    ;

filter [String context]
    : about_clause[context]
    | arithmetic_clause[context]
    | #(AND filter[context] filter[context])
    | #(OR filter[context] filter[context])
    ;

filtered_path [String context] returns [String p=""]
    : #(LANGLE p=path filter[context + " " + p])
    | p=path
    ;

query
{ String p1, p2 = ""; }
    : co[""]
    | #(CAS p1=filtered_path[""] (p2=filtered_path[p1])?) {
        processor.setTargetPath(p1 + p2);
    }
    ;

/*
attr
    : ATTRIBUTE^ tag
    ;
*/
/*
Copyright (c) 2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/